Option Strict On

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Access
Imports System.Xml
Imports System.IO
Imports System.Xml.Serialization
Imports System.Runtime.InteropServices


Public Class UpdateAccessDBFromSVN
    Inherits CreateAccessDBFromSVN

    Public Overrides Sub Transform()

        Dim accFilename As String = Me.getFinalAccessFilename

        Call raiseTransformStarted()
        Try
            Me.openDatabase()
            Me.LoadObjectList()
            If Me.ObjectList.Count = 0 Then
                Console.WriteLine("No changes since last update of database file: " & accFilename)

            Else
                Me.TransformObjects()
            End If

            Dim revno As String = CStr(Me.SvnClient.getSVNCurrentRevision())
            If CInt(revno) = 0 Then
                Throw New ApplicationException("Can;t update revision property to ""0""")
            End If

            Call AppContext.current.AddOrChangeAccessProperty(Me.app, "svnRevision", revno)
           
            Me.app.CloseCurrentDatabase()
            Console.WriteLine("Updated database file: " & accFilename & " to revision:" & revno)

        Catch e As Exception
            Me.raiseTransformError(e.Message)
        Finally

            If Me.app IsNot Nothing Then

                Me.app.Quit()

                GC.Collect()
                GC.WaitForPendingFinalizers()

                If Me.app IsNot Nothing Then Marshal.ReleaseComObject(Me.app)
                Me.app = Nothing

            End If
            Call raiseTransformEnd()

        End Try

       

    End Sub

    Protected Overrides Sub openDatabase()

        Me.app = New Access.Application
        Me.app.Visible = False
        Me.app.UserControl = False

        Console.WriteLine("Access Application version:" & Me.app.Version & ", build " & Me.app.Build)

        Dim dbFilename As String = Me.getFinalAccessFilename()
        If dbFilename.ToLower.EndsWith("adp") Then
            Me.app.OpenAccessProject(dbFilename, False)
        Else
            Me.app.OpenCurrentDatabase(dbFilename, False)
        End If

    End Sub

    Protected Overrides Sub LoadObjectList()

        Me.ObjectList.AddRange(Me.SvnClient.SvnUpdateAndGetImportedFileList)

    End Sub


End Class
